var ajaxDataDevice = {
    pageNumber: 1
};
var ajaxDataCheck = {
    pageNumber: 1
};
var ajaxDataReport = {
    pageNumber: 1
};
var ajaxDataRepair = {
    pageNumber: 1
};

$.ajaxSetup({
    type: 'POST',
    dataType: 'json',
    timeout: 1000000,
    error: function (xhr) {
        throw new Error(xhr.status + ':' + xhr.statusText);
    }
});

//获取path信息
var getvarJson = function() {
    $.ajax({
        type: 'POST',
        url: 'http://weixin.yiyikefu.com/index.php?m=weixin&c=manage&a=getvars',
        dataType: 'json',
        error: function (xhr) {
            throw new Error('错误代码:' + xhr.status);
        },
        success: function (response) {
            var path=response.url;
            var heng_id = response.heng_id
            var huser_id = response.huser_id;
            if(path && heng_id){
                getBaseJson(path,heng_id,huser_id);
                getDeviceInfo(path,heng_id);
                getCheckInfo(path,heng_id);
                getReportInfo(path,heng_id);
                getRepairInfo(path,heng_id)
            }
        }
    });    
};
getvarJson();

//获取基础信息
var getBaseJson = function(path,heng_id,huser_id) {
    $.ajax({
        url: path + 'engineer/engBaseInf?api_id=wx833&api_sign=wx885b3e4e8c7b4d89e01e&heng_id='+heng_id,
        success: function(res) {
           showBaseInfo(res.body);
        }
    });
};

//获取设备信息
var getDeviceInfo = function(path,heng_id) {
    $.ajax({
        url: path + 'engineer/engRelateDevice?api_id=wx833&api_sign=wx885b3e4e8c7b4d89e01e&heng_id='+heng_id,
        success: function(res) {
            showTableInfo(res.list, 'device');
            createPagination(res, 'device');
        }
    });
};

//获取待检信息
var getCheckInfo = function(path,heng_id) {
    $.ajax({
        url: path + 'engineer/engShouldTestDevice?api_id=wx833&api_sign=wx885b3e4e8c7b4d89e01e&heng_id='+heng_id,
        success: function(res) {
            showTableInfo(res.list, 'check');
            createPagination(res, 'check');
        }
    });
};

//获取报修信息
var getReportInfo = function(path,heng_id) {
    $.ajax({
        url: path + 'engineer/showDeviceReportByEng?api_id=wx833&api_sign=wx885b3e4e8c7b4d89e01e&heng_id='+heng_id,
        success: function(res) {
            showTableInfo(res.body.list, 'report');
            createPagination(res.body, 'report');
        }
    });
};

//获取维修信息
var getRepairInfo = function(path,heng_id) {
    $.ajax({
        url: path + 'engineer/showDeviceRepairByEng?api_id=wx833&api_sign=wx885b3e4e8c7b4d89e01e&heng_id='+heng_id,
        success: function(res) {
            showTableInfo(res.list, 'repair');
            createPagination(res, 'repair');
        }
    });
};

var showBaseInfo = function(res) {

    for(var i in res) {
        if(!res[i]) {
            res[i] = '无';
        }
    }
    $('#heng_name').html(res.heng_name);
    $('#heng_phone').html(res.heng_phone);
    switch (res.heng_status) {
        case 1:
            res.heng_status = '正常工作';
            break;
        case 2:
            res.heng_status = '休假';
            break;
        case 3:
            res.heng_status = '离职或换岗';
            break;
    }
    $('#heng_status').html(res.heng_status);
    
    if(res.heng_status_freeze=="2"){
        $('#heng_status_freeze').html("是");
    }else if(res.heng_status_freeze=="1"){
        $('#heng_status_freeze').html("否");
    }
};

var showTableInfo = function(res, name) {
    var tbody = [];
    switch (name) {
        case 'device':
            for (var i = 1; i <= res.length; i++) {
                var hdevice_name = res[i - 1].hdevice_name;
                var hdevice_status = res[i - 1].hdevice_status;
                
                
                if(hdevice_status=="1"){
                    hdevice_status="正常";
                }else if(hdevice_status=="2"){
                    hdevice_status="已报修";
                }else if(hdevice_status=="3"){
                    hdevice_status="淘汰";
                }
                
                var hdepart_name = res[i - 1].hdepart_name;
                tbody.push('' + '<tr id="tr' + i + '">' +
                '<td id="hdevice_name' + i + '">' + hdevice_name + '</td>' +
                '<td id="hdevice_status' + i + '">' + hdevice_status + '</td>' +
                '<td id="hdepart_name' + i + '">' + hdepart_name + '</td>' +
                '</tr>');
            }
            break;
        case 'check':
            for (var i = 1; i <= res.length; i++) {
                var hdevice_name = res[i - 1].hdevice_name;
                var htests_name = res[i - 1].htests_name;
                var htip_nextchecktime = res[i - 1].htip_nextchecktime;
                tbody.push('' + '<tr id="tr' + i + '">' +
                '<td id="hdevice_name' + i + '">' + hdevice_name + '</td>' +
                '<td id="htests_name' + i + '">' + htests_name + '</td>' +
                '<td id="htip_nextchecktime' + i + '">' + htip_nextchecktime + '</td>' +
                '</tr>');
            }
            break;
        case 'report':
            for (var i = 1; i <= res.length; i++) {
                var hdevice_name = res[i - 1].hdevice_name;
                var hreport_name = res[i - 1].hreport_name;
                var hreport_time = res[i - 1].hreport_time;
                tbody.push('' + '<tr id="tr' + i + '">' +
                '<td id="hdevice_name' + i + '">' + hdevice_name + '</td>' +
                '<td id="hreport_name' + i + '">' + hreport_name + '</td>' +
                '<td id="hreport_time' + i + '">' + hreport_time + '</td>' +
                '</tr>');
            }
            break;
        case 'repair':
            for (var i = 1; i <= res.length; i++) {
                var hdevice_name = res[i - 1].hdevice_name;
                var hrepair_status = res[i - 1].hrepair_status;
                switch (hrepair_status) {
                    case 1:
                        break;
                    case 2:
                        break;
                }
                var hrepair_time = res[i - 1].hrepair_time;
                tbody.push('' + '<tr id="tr' + i + '">' +
                '<td id="hdevice_name' + i + '">' + hdevice_name + '</td>' +
                '<td id="hrepair_status' + i + '">' + hrepair_status + '</td>' +
                '<td id="hrepair_time' + i + '">' + hrepair_time + '</td>' +
                '</tr>');
            }
            break;
    };
    //清空
    $('.' + name + 'Info tbody').empty();
    //动态生成
    $('.' + name + 'Info tbody').html(tbody.join(''));
};

var createPagination = function (json, name) {
    // 当前页数
    var pageNumber = json.pageNumber;
    // 总页数
    var totalPage = json.totalPage;
    if (totalPage == 0)
        totalPage = 1;
    if (totalPage < pageNumber) {
        ajaxData.pageNumber = totalPage;
        getJson();
        return;
    }
    var select = [];
    for (var i = 1; i <= totalPage; i++) {
        select.push('<option value="' + i + '">' + i + '</option>');
    }
    $('#' + name + 'Select').html('');
    $('#' + name + 'Select').html(select.join(''));
    $('#' + name + 'Select').val(pageNumber);
    $('#' + name + 'Total').html(totalPage);
    // select初始化
    $('select').material_select();
    bindPagination(json, name);
};
var bindPagination = function (json, name) {
    $('.' + name + 'Info .pagination select').unbind('change');
    $('.' + name + 'Info .pagination select').on('change', function () {
        switch (name) {
            case 'device':
                ajaxDataDevice.pageNumber = $(this).val();
                getCheckInfo();
                break;
            case 'check':
                ajaxDataCheck.pageNumber = $(this).val();
                getCheckInfo();
                break;
            case 'report':
                ajaxDataReport.pageNumber = $(this).val();
                getReportInfo();
                break;
            case 'repair':
                ajaxDataRepair.pageNumber = $(this).val();
                getRepairInfo();
                break;
        }
    });

    $('.' + name + 'Info .pagination .prev').unbind('click');
    $('.' + name + 'Info .pagination .prev')
        .click(
        function () {
            switch (name) {
                case 'device':
                    ajaxDataDevice.pageNumber = ajaxDataDevice.pageNumber < 1 ? 1
                        : json.pageNumber - 1;
                    getDeviceInfo();
                    break;
                case 'check':
                    ajaxDataCheck.pageNumber = ajaxDataCheck.pageNumber < 1 ? 1
                        : json.pageNumber - 1;
                    getCheckInfo();
                    break;
                case 'report':
                    ajaxDataReport.pageNumber = ajaxDataReport.pageNumber < 1 ? 1
                        : json.pageNumber - 1;
                    getReportInfo();
                    break;
                case 'repair':
                    ajaxDataRepair.pageNumber = ajaxDataRepair.pageNumber < 1 ? 1
                        : json.pageNumber - 1;
                    getRepairInfo();
                    break;
            }

        });

    $('.' + name + 'Info .pagination .next').unbind('click');
    $('.' + name + 'Info .pagination .next')
        .click(
        function () {
            switch (name) {
                case 'device':
                    ajaxDataDevice.pageNumber = ajaxDataDevice.pageNumber > json.totalPage ? json.totalPage
                        : json.pageNumber + 1;
                    getDeviceInfo();
                    break;
                case 'check':
                    ajaxDataCheck.pageNumber = ajaxDataCheck.pageNumber > json.totalPage ? json.totalPage
                        : json.pageNumber + 1;
                    getCheckInfo();
                    break;
                case 'report':
                    ajaxDataReport.pageNumber = ajaxDataReport.pageNumber > json.totalPage ? json.totalPage
                        : json.pageNumber + 1;
                    getReportInfo();
                    break;
                case 'repair':
                    ajaxDataRepair.pageNumber = ajaxDataRepair.pageNumber > json.totalPage ? json.totalPage
                        : json.pageNumber + 1;
                    getRepairInfo();
                    break;
            }
        });
};

//下拉列表初始化
$('select').material_select();